<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-bottom: 10px" type="primary" icon="el-icon-circle-plus" @click="addLevelOne">新增分类</el-button>
    </div>
    <el-table :data="menuData" style="width: 100%;margin-bottom: 20px;" row-key="id" border default-expand-all :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column prop="name" label="菜单名称" />
      <el-table-column prop="type" label="类型">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 0">一级菜单</span>
          <span v-else>二级菜单</span>
        </template>
      </el-table-column>
      <el-table-column prop="menuSort" label="排序" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="success" size="small" icon="el-icon-edit" @click="getMenu(scope.row.id)">编辑</el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="deleteMenu(scope.row.id)">删除</el-button>
          <el-button v-if="scope.row.type == 0" type="primary" size="small" icon="el-icon-plus" @click="addLevelTow(scope.row)">新增子菜单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :model="dataForm" :rules="rules" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item v-if="showLevelOneName" label="一级菜单" prop="parentName">
          <el-input v-model="dataForm.parentName" :disabled="true" />
        </el-form-item>
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="dataForm.name" />
        </el-form-item>
        <el-form-item label="排序" prop="menuSort">
          <el-input v-model="dataForm.menuSort" type="number" />
        </el-form-item>
        <el-form-item v-if="showIcons" label="菜单图标" prop="icons">
          <el-input v-model="dataForm.icons" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus.indexOf('create') > -1" type="primary" @click="save('dataForm')">添加</el-button>
        <el-button v-else type="primary" @click="save('dataForm')">更新</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, update, get, del } from '@/api/helpcenter/menu'
import { objectToFormData } from '@/utils'
export default {
  data() {
    return {
      menuData: [],
      dialogStatus: '',
      textMap: {
        createLevelOne: '新增一级菜单',
        updateLevelOne: '更新一级菜单',
        createLevelTow: '新增二级菜单',
        updateLevelTow: '更新二级菜单'
      },
      dataForm: {
        id: '',
        icons: '',
        menuSort: '',
        name: '',
        parentName: '',
        parentId: 0,
        type: 0
      },
      dialogFormVisible: false,
      showLevelOneName: false,
      showIcons: true,
      rules: {
        name: [
          { required: true, message: '请输入菜单名称' }
        ],
        menuSort: [
          { required: true, message: '请输入排序' }
        ],
        icons: [
          { required: true, message: '请输入字体图标' }
        ]
      }
    }
  },
  watch: {
    'dataForm.type': function(newVal) {
      console.log(newVal)
      if (newVal === 0) {
        this.showLevelOneName = false
        this.showIcons = true
      } else if (newVal === 1) {
        this.showLevelOneName = true
        this.showIcons = false
      }
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData() {
      const other = this
      getList().then(response => {
        other.menuData = response.data
      })
    },
    resetForm() {
      this.dataForm = {
        id: '',
        icons: '',
        menuSort: '',
        name: '',
        parentName: '',
        parentId: 0,
        type: 0
      }
    },
    addLevelOne() {
      this.resetForm()
      debugger
      this.dialogFormVisible = true
      this.dialogStatus = 'createLevelOne'
    },
    addLevelTow(row) {
      this.resetForm()
      this.dataForm.parentName = row.name
      this.dataForm.parentId = row.id
      this.dataForm.type = 1
      this.dialogFormVisible = true
      this.dialogStatus = 'createLevelTow'
    },
    getMenu(id) {
      const other = this
      other.dialogFormVisible = true
      get(id).then(response => {
        other.dataForm.id = response.data.id
        other.dataForm.icons = response.data.icons
        other.dataForm.menuSort = response.data.menuSort
        other.dataForm.name = response.data.name
        other.dataForm.parentName = response.data.parentName
        other.dataForm.parentId = response.data.parentId
        other.dataForm.type = response.data.type

        if (other.dataForm.type === 0) {
          other.dialogStatus = 'updateLevelOne'
        } else {
          other.dialogStatus = 'updateLevelTow'
        }
      })
    },
    save(formName) {
      const other = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          other.dialogFormVisible = false
          update(objectToFormData(other.dataForm)).then(response => {
            other.$message.success(response.msg)
            other.getData()
            other.$refs[formName].resetFields()
            other.resetForm()
          })
        }
      })
    },
    deleteMenu(id) {
      const other = this
      const params = {}
      params.id = id
      this.$confirm('确认删除该菜单', { type: 'warning', confirmButtonText: '确认', cancelButtonText: '取消' })
        .then(_ => {
          del(params).then(response => {
            if (response.code === 0) {
              other.getData()
              other.$message.success(response.msg)
            } else {
              other.$message.error(response.msg)
            }
          })
        })
        .catch(_ => {
          other.$message.error('操作失败')
        })
    }
  }
}
</script>

<style scoped>

</style>
